import wx, { Component, PropTypes } from 'labrador-immutable';
import { bindActionCreators } from 'redux';
import { connect } from 'labrador-redux';
import { createAction } from 'redux-actions';
import Todo from '../../components/todo/todo';
import * as todoActions from '../../redux/todos';
import { sleep } from '../../utils/utils';

import BriefCard from '../../components/briefcard/briefcard';

const { array, func } = PropTypes;

class Index extends Component {
    static propTypes = {
        todos: array,
        removeTodo: func,
        restoreTodo: func,
        createTodo: func,
        finishTodo: func
    };

    state = {
        winHeight: '550px',
        goodCate: 0,
    };

    children() {
        let cards = this.props.list || {};
        return {
            cards: cards.map((card) => ({
                component: BriefCard,
                key: card.id,
                props: {
                    ...card
                }
            })),
        }
    }

    handChangeCate() {
        let goodCate
        if (this.state.goodCate == 1) {
            goodCate = 0
            this.setState({ goodCate: 0 })
        } else if (this.state.goodCate == 0) {
            goodCate = 1
            this.setState({ goodCate: 1 })
        }
        this.fetchPosts(0, goodCate)
    }

    onLoad() {
        let that = this;
        // setTimeout(() => {
        //   // this.requestMenuList('load');
        // }, 200);
        wx.getSystemInfo({
            complete: function(res) {
                let height = that.isIOS(res.system) ? res.windowHeight : res.windowHeight + 50
                that.setState({
                    winHeight: height + 'px'
                })
            }
        })
        this.fetchPosts(0, 0);
        this.props.checkLoginStatus();
    }

    async fetchPosts(pageNum, goodCate) {
        this.props.fetchPosts({
            pageNum: pageNum,
            goodCate: goodCate
        });
    }

    isIOS(system) {
        return system.indexOf('iOS') >= 0
    }

    onUpdate(props) {}

    onReachBottom() {
        let { pageNum, end } = this.props;
        if (end) {
            return;
        }
        pageNum++;
        this.fetchPosts(pageNum, this.state.goodCate);
    }

    onPullDownRefresh() {
        this.fetchPosts(0, this.state.goodCate);
        setTimeout(() => {
            wx.hideNavigationBarLoading();
            wx.stopPullDownRefresh();
        }, 1000);
    }

    // 设置分享
    onShareAppMessage() {
        return {
            title: this.props.info.title,
            desc: '萌酱酱选辅食一个致力于帮助新手爸妈为宝宝轻松制作辅食的APP。',
            path: '/pages/index/index',
        }
    }

}

export default connect(
    ({ post }) => ({ list: post.list, pageNum: post.pageNum, end: post.end }),
    (dispatch) => bindActionCreators({
        checkLoginStatus: createAction('user/checkLoginStatus', (params) => (params)),
        fetchPosts: createAction('post/fetchPostList', (params) => (params))
    }, dispatch)
)(Index);